package com.mindata.blockchain.core.model.convert;

import com.mindata.blockchain.core.model.MessageEntity;
import com.mindata.blockchain.core.model.SyncEntity;
import com.mindata.blockchain.core.model.base.BaseEntity;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 表名和实体类的对应
 * @author wuweifeng wrote on 2018/3/2.
 */
@Component
public class ConvertTableName<T extends BaseEntity> {
	private Map<String, Class<T>> metaMap = new ConcurrentHashMap<>();

	@SuppressWarnings("unchecked")
	public ConvertTableName() {
		this.metaMap.put("message", (Class<T>) MessageEntity.class);
		this.metaMap.put("sync", (Class<T>) SyncEntity.class);
	}

	/**
	 * 根据表名获取class名
	 * @return 表对应的实体类
	 */
	public Class<T> convertOf(String tableName) {
		return metaMap.get(tableName);
	}

	/**
	 * 根据类名取表名
	 * @param clazz 类名
	 * @return 表名
	 */
	public String convertOf(Class<T> clazz) {
		for (String key : metaMap.keySet()) {
			if (metaMap.get(key).equals(clazz)) {
				return key;
			}
		}
		return null;
	}
}
